#!/bin/sh

#数据库地址
DATASTRING="jdbc:mysql://${DBHOST}:${DBPORT}/${DBNAME}?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true"
DATAUSERNAME=$DBUSERNAME
DATAPASSWORD=$DBPWD

THISXMSIZE=$XM
THISJAR_FILE=${JAR_FILE}
THISMANAGEMENT_SERVER_PORT=$MANAGEMENT_SERVER_PORT


THIS_JC_AUTH_K=${JC_AUTH_K}
THIS_JC_AUTH_KEY=${JC_AUTH_KEY}
THIS_JC_AUTH_N=${JC_AUTH_N}

[ -z "$XM" ] && {
    THISXMSIZE="2024M"
}

[ -z "$JAR_FILE" ] && {
    THISJAR_FILE="app.jar"
}

[ -z "$MANAGEMENT_SERVER_PORT" ] && {
    THISMANAGEMENT_SERVER_PORT="-1"
}

[ -z "$JC_AUTH_K" ] && {
    THIS_JC_AUTH_K="seres"
}
[ -z "$JC_AUTH_KEY" ] && {
    THIS_JC_AUTH_KEY="brx.auth.value"
}
[ -z "$JC_AUTH_N" ] && {
    THIS_JC_AUTH_N="bGKH6HLVD1ZwVJhtuidCszdGhAxM0cBz0Wuc+1G6FENEcdHR0hC2AWUITmffzq/9pof3dk3Hoe3ZHuM3RMvvS27Y6NNNJZZ8LL2gX8AthCKhI7LL4GqnKSI/IFCe5Z1U"
}




echo -e "数据库连接字符串>>${DATASTRING}"
echo -e "NACOS>>${NACOS_HOST}:${NACOS_PORT}"
echo -e "启动端口>>${EXPOSE_PORT}"

DUBBO_REGISTRY_ADDRESS="-Ddubbo.registry.address=nacos://${NACOS_HOST}:${NACOS_PORT}"

#运行key
# KEYVALUE=$(java -jar ./brx-key.jar)

# echo $KEYVALUE

# NEWKEYVALUE=${KEYVALUE#*:}


JAVA_OPTS="-server -XX:SurvivorRatio=8 -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m -XX:+UseParallelGC -XX:ParallelGCThreads=4 -XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/ -Xloggc:/app/brx/log/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=10M"

#定义基础运行命令
# RUNCMD="java -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai ${JAVA_OPTS} ${THISXM}-Xmx${THISXMSIZE} -Xms${THISXMSIZE} -Dbrx.auth.value=${NEWKEYVALUE} -Dmanagement.endpoints.enabled-by-default=false"
#key使用固定值
RUNCMD="java -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai ${JAVA_OPTS} ${THISXM}-Xmx${THISXMSIZE} -Xms${THISXMSIZE} -Djc.auth.k=${THIS_JC_AUTH_K} -Djc.auth.key=${THIS_JC_AUTH_KEY} -Djc.auth.n=${THIS_JC_AUTH_N} -Dmanagement.endpoints.enabled-by-default=false"


#PROFILES
[ -n "${PROFILES}" ] && {
    RUNCMD="${RUNCMD} -Dspring.profiles.active=${PROFILES}"
}

#nacos NACOS_HOST
[ -n "${NACOS_HOST}" ] && {
    RUNCMD="${RUNCMD} -Dspring.cloud.nacos.config.server-addr=${NACOS_HOST}:${NACOS_PORT} -Dspring.cloud.nacos.discovery.server-addr=${NACOS_HOST}:${NACOS_PORT}"
}
#end



#nacos namespace
[ -n "${NACOSNAMESPACE}" ] && {
    RUNCMD="${RUNCMD} -Dspring.cloud.nacos.discovery.namespace=${NACOSNAMESPACE} -Ddubbo.registry.parameters.namespace=${NACOSNAMESPACE} -Dspring.cloud.nacos.config.namespace=${NACOSNAMESPACE}"
}
#end

#nacos NACOS_USERNAME
[ -n "${NACOS_USERNAME}" ] && {
    RUNCMD="${RUNCMD} -Ddubbo.registry.username=${NACOS_USERNAME} -Dspring.cloud.nacos.config.username=${NACOS_USERNAME} -Dspring.cloud.nacos.discovery.username=${NACOS_USERNAME}"
    DUBBO_REGISTRY_ADDRESS="${DUBBO_REGISTRY_ADDRESS}?username=${NACOS_USERNAME}"
}
#end
#nacos NACOS_PASSWORD
[ -n "${NACOS_PASSWORD}" ] && {
    RUNCMD="${RUNCMD} -Ddubbo.registry.password=${NACOS_PASSWORD} -Dspring.cloud.nacos.config.password=${NACOS_PASSWORD} -Dspring.cloud.nacos.discovery.password=${NACOS_PASSWORD}"
    DUBBO_REGISTRY_ADDRESS="${DUBBO_REGISTRY_ADDRESS}&password=${NACOS_PASSWORD}"
}
#end

#WEBHOST
[ -n "${WEBHOST}" ] && {
    RUNCMD="${RUNCMD} -Dhost.webhost=${WEBHOST}"
}
#end

#DATAUSERNAME
[ -n "${DATAUSERNAME}" ] && {
    RUNCMD="${RUNCMD} -Dspring.datasource.username=${DATAUSERNAME}"
}
#end

#DATAPASSWORD
[ -n "${DATAPASSWORD}" ] && {
    RUNCMD="${RUNCMD} -Dspring.datasource.password=${DATAPASSWORD}"
}
#end

#DATASTRING
[ -n "${DATAUSERNAME}" ] && {
    [ -n "${DATAPASSWORD}" ] && {
        RUNCMD="${RUNCMD} -Dspring.datasource.url=${DATASTRING}"
    }
}
#end

#REDIS_HOST
[ -n "${REDIS_HOST}" ] && {
    RUNCMD="${RUNCMD} -Dspring.redis.host=${REDIS_HOST}"
}
#end

#REDIS_PORT
[ -n "${REDIS_PORT}" ] && {
    RUNCMD="${RUNCMD} -Dspring.redis.port=${REDIS_PORT}"
}
#end

#REDIS_PASSWORD
[ -n "${REDIS_PASSWORD}" ] && {
    RUNCMD="${RUNCMD} -Dspring.redis.password=${REDIS_PASSWORD}"
}
#end

#REDIS_USERNAME
[ -n "${REDIS_USERNAME}" ] && {
    RUNCMD="${RUNCMD} -Dspring.redis.username=${REDIS_USERNAME}"
}
#end

#REDIS_DATABASE
[ -n "${REDIS_DATABASE}" ] && {
    RUNCMD="${RUNCMD} -Dspring.redis.database=${REDIS_DATABASE}"
}
#end

#LOCALHOST_HOST
[ -n "${LOCALHOST_HOST}" ] && {
    RUNCMD="${RUNCMD} -DDUBBO_IP_TO_REGISTRY=${LOCALHOST_HOST} -Dspring.cloud.nacos.discovery.ip=${LOCALHOST_HOST}"
}
#end

#OSS_ENDPOINT
[ -n "${OSS_ENDPOINT}" ] && {
    RUNCMD="${RUNCMD} -Djc.oss.endpoint=${OSS_ENDPOINT}"
}
#end

#OSS_ACCESS_KEY
[ -n "${OSS_ACCESS_KEY}" ] && {
    RUNCMD="${RUNCMD} -Djc.oss.accessKey=${OSS_ACCESS_KEY}"
}
#end

#OSS_SECRET_KEY
[ -n "${OSS_SECRET_KEY}" ] && {
    RUNCMD="${RUNCMD} -Djc.oss.secretKey=${OSS_SECRET_KEY}"
}
#end

#OSS_TYPE
[ -n "${OSS_TYPE}" ] && {
    RUNCMD="${RUNCMD} -Djc.oss.type=${OSS_TYPE}"
}
#end
#OSS_TARGET
[ -n "${OSS_TARGET}" ] && {
    RUNCMD="${RUNCMD} -Djc.oss.target=${OSS_TARGET}"
}
#end
#OSS_LINK
[ -n "${OSS_LINK}" ] && {
    RUNCMD="${RUNCMD} -Djc.oss.link=${OSS_LINK}"
}
#end

#OSS_BUCKET_NAME
[ -n "${OSS_BUCKET_NAME}" ] && {
    RUNCMD="${RUNCMD} -Djc.oss.bucketName=${OSS_BUCKET_NAME}"
}
#end

#OSS_TYPE_WHITE
[ -n "${OSS_TYPE_WHITE}" ] && {
    RUNCMD="${RUNCMD} -Djc.oss.typeWhiteList=${OSS_TYPE_WHITE}"
}
#end

#plumelog
[ -n "${PLUMELOGENABLED}" ] && {
    RUNCMD="${RUNCMD} -Djc.plumelog.enabled=${PLUMELOGENABLED} -Djc.plumelog.model=${PLUMELOGMODEL}"
    # 判断环境变量PLUMELOGMODEL
    if [ "$PLUMELOGMODEL" = "lite" ]; then
        [ -n "${PLUMELOGHOST}" ] && {
            RUNCMD="${RUNCMD} -Djc.plumelog.host=${PLUMELOGHOST}"
        }
    elif [ "$PLUMELOGMODEL" = "redis" ]; then
        [ -n "${PLUMELOGREDISHOST}" ] && {
            RUNCMD="${RUNCMD} -Djc.plumelog.redis.host=${PLUMELOGREDISHOST}"
        }
        [ -n "${PLUMELOGREDISPORT}" ] && {
            RUNCMD="${RUNCMD} -Djc.plumelog.redis.port=${PLUMELOGREDISPORT}"
        }
        [ -n "${PLUMELOGREDISPASSWORD}" ] && {
            RUNCMD="${RUNCMD} -Djc.plumelog.redis.password=${PLUMELOGREDISPASSWORD}"
        }
        [ -n "${PLUMELOGREDISDB}" ] && {
            RUNCMD="${RUNCMD} -Djc.plumelog.redis.database=${PLUMELOGREDISDB}"
        }
    elif [ "$PLUMELOGMODEL" = "kafka" ]; then
        [ -n "${PLUMELOGKAFKAHOSTS}" ] && {
            RUNCMD="${RUNCMD} -Djc.plumelog.kafka.host=${PLUMELOGKAFKAHOSTS}"
        }
        [ -n "${PLUMELOGKAFKAGROUPNAME}" ] && {
            RUNCMD="${RUNCMD} -Djc.plumelog.redis.groupname=${PLUMELOGKAFKAGROUPNAME}"
        }
    else
        echo "PLUMELOGMODEL does not equal 1"
    fi
}
#end

#xxljob
[ -n "${XXLJOBEXECUTORADDRESS}" ] && {
    RUNCMD="${RUNCMD} -Djc.job.admin.addresses=${XXLJOBEXECUTORADDRESS}"
}
[ -n "${XXLJOBACCESSTOKEN}" ] && {
    RUNCMD="${RUNCMD} -Djc.job.admin.accessToken=${XXLJOBACCESSTOKEN} -Djc.job.accessToken=${XXLJOBACCESSTOKEN}"
}
[ -n "${XXLJOBEXECUTORAPPNAME}" ] && {
    RUNCMD="${RUNCMD} -Djc.job.executor.appname=${XXLJOBEXECUTORAPPNAME}"
}
[ -n "${XXLJOBEXECUTORIP}" ] && {
    RUNCMD="${RUNCMD} -Djc.job.executor.ip=${XXLJOBEXECUTORIP}"
}
[ -n "${XXLJOBEXECUTORPORT}" ] && {
    RUNCMD="${RUNCMD} -Djc.job.executor.port=${XXLJOBEXECUTORPORT}"
}
#end

[ -n "${PROJECT_NAME}" ] && {
    RUNCMD="${RUNCMD}  -Djc.project.name=${PROJECT_NAME}"
}

[ -n "${JC_JASYPT_ENCRYPTOR_PASSWORD}" ] && {
    RUNCMD="${RUNCMD}  -Djasypt.encryptor.password=${JC_JASYPT_ENCRYPTOR_PASSWORD}"
}


[ -n "${COMMON_ENV}" ] && {
    RUNCMD="${RUNCMD}  ${COMMON_ENV}"
}

RUNCMD="${RUNCMD} ${DUBBO_REGISTRY_ADDRESS} "

[ -n "${THISMANAGEMENT_SERVER_PORT}" ] && {
    RUNCMD="${RUNCMD} -Dmanagement.server.port=${THISMANAGEMENT_SERVER_PORT}"
}

# echo $NEWKEYVALUE

RUNCMD="${RUNCMD} -jar ./${THISJAR_FILE}"

[ -n "${EXPOSE_PORT}" ] && {
    RUNCMD="${RUNCMD} --server.port=${EXPOSE_PORT}"
}

echo "最终执行的命令： $RUNCMD"


nohup ${RUNCMD}
# ##在脚本最后一行添加tail -f /dev/null，这个命令永远完成不了，所以该脚本一直不会执行完，所以该容器永远不会退出
# tail -f /dev/null